package jsyf.oa.dao.wreport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.entity.wreport.WReportUnfinishMainEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 条件查询用户列表
 * @author: @张巨波 @zhangjubo
 * @creat: on 2017/12/6.
 * @since: V1.0.0
 * @see jsyf.oa.web
 */
@Repository
public class WReportUnfinishMainDao extends BaseDao {
    public WReportUnfinishMainDao() {
        super.iBaseEntity = new WReportUnfinishMainEntity();
    }

    public List<WReportUnfinishMainEntity> getEntityListByCondition(String condition, Object[] params) {
        List<WReportUnfinishMainEntity> entityList = new ArrayList<>();
        List<IBaseEntity> iBaseEntityList = super.getIBaseEntityByCondition(condition, params);
        for (int i = 0; i < iBaseEntityList.size(); i++) {
            entityList.add((WReportUnfinishMainEntity) iBaseEntityList.get(i));
        }
        return entityList;
    }

    public WReportUnfinishMainEntity getEntityByCondition(String condition, Object[] params) {
        List<WReportUnfinishMainEntity> entityList = new ArrayList<>();
        List<IBaseEntity> iBaseEntityList = super.getIBaseEntityByCondition(condition, params);
        if (iBaseEntityList.size() == 0) {
            return null;
        } else {

            for (int i = 0; i < iBaseEntityList.size(); i++) {
                entityList.add((WReportUnfinishMainEntity) iBaseEntityList.get(i));
            }
            return entityList.get(0);
        }
    }

    public List<WReportUnfinishMainEntity> getFinishEntityListByCondition(String condition, Object[] params) {
        List<WReportUnfinishMainEntity> entityList = new ArrayList<>();
        List<IBaseEntity> iBaseEntityList = super.getIBaseEntityByCondition(condition, params);
        for (int i = 0; i < iBaseEntityList.size(); i++) {
            entityList.add((WReportUnfinishMainEntity) iBaseEntityList.get(i));
        }
        return entityList;
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, WReportUnfinishMainEntity.class);
    }

    /**
     * 获取公司列表及未关闭事项个数
     *
     * @return
     */
    public List<JSONObject> getWReportUnfinishCount() {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT a.ID,a.`NAME`, ");
        sb.append("(select COUNT(*) from T_WRT_UNFINISH_MAIN where USER_ID IN (SELECT ID FROM V_SYS_USER WHERE companyId=a.id) ");
        sb.append("AND JOB_STATE='0' AND TYPE='3' AND TRACK_CONTENT IS NOT NULL AND JINZHAN IS NOT NULL ");
        sb.append(") as UnFinishCount from T_SYS_COMPANY as a ");
        return queryForJsonList(sb.toString());
    }

    /**
     * 获取未关闭事项列表
     *
     * @param conditon
     * @return
     */
    public List<JSONObject> getWReportUnfinishMainList(String conditon) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT B.NAME USER_NAME,C.NAME PRINCIPAL_NAME,A.* from T_WRT_UNFINISH_MAIN A  ");
        sb.append("lEFT JOIN T_SYS_USER B ON A.USER_ID=B.ID ");
        sb.append("lEFT JOIN T_SYS_USER C ON A.PRINCIPAL=C.ID ");
        sb.append("lEFT JOIN v_sys_company_user D ON A.USER_ID=D.userId ");
        sb.append("WHERE A.TYPE='3' AND A.TRACK_CONTENT IS NOT NULL  ");
        sb.append("AND A.JINZHAN IS NOT NULL AND A.DEL_FLAG=0 ");
        sb.append(conditon);
        return queryForJsonList(sb.toString());
    }

    /**
     * 获取未关闭事项条数
     *
     * @param conditon
     * @return
     */
    public Integer getWReportUnfinishMainListCount(String conditon) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT COUNT(*) AS recordCount from T_WRT_UNFINISH_MAIN A ");
        sb.append("lEFT JOIN v_sys_company_user D ON A.USER_ID=D.userId ");
        sb.append("WHERE A.TYPE='3'");
        sb.append("AND A.TRACK_CONTENT IS NOT NULL AND A.JINZHAN IS NOT NULL AND A.DEL_FLAG=0 ");
        sb.append(conditon);
        return Integer.valueOf(queryForJsonObject(sb.toString()).get("recordCount").toString());
    }

    /**
     * 根据未关闭事项Id获取跟踪列表
     *
     * @param mainId
     * @return
     */
    public List<JSONObject> getWReportUnfinishListByMainId(String mainId) {
        StringBuffer sb = new StringBuffer();
        sb.append("select * ");
        sb.append("from T_WRT_UNFINISH as a WHERE a.MAIN_ID='" + mainId + "' ");
        return queryForJsonList(sb.toString());
    }

    /**
     * 获取未关闭事项列表的最新跟踪信息
     *
     * @param conditon
     * @return
     */
    public List<JSONObject> getWReportUnfinishList(String conditon) {
        StringBuffer sb = new StringBuffer();
        sb.append("select a.* ");
        sb.append("from T_WRT_UNFINISH a,(  ");
        sb.append("     select MAIN_ID,max(create_time) create_time from T_WRT_UNFINISH group by MAIN_ID  ");
        sb.append(") b where a.MAIN_ID = b.MAIN_ID and a.create_time = b.create_time and a.JINZHAN is not null  ");
        sb.append("and a.MAIN_ID IN ");
        sb.append("(SELECT ID from T_WRT_UNFINISH_MAIN WHERE JOB_STATE='0' AND TYPE='3'AND TRACK_CONTENT IS NOT NULL ");
        sb.append("AND JINZHAN IS NOT NULL)  ");
        return queryForJsonList(sb.toString());
    }

    public List<JSONObject> getUnfinishPersonList(String date) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT B.ID,B.NAME FROM ( ");
        sb.append("        select  PRINCIPAL from t_wrt_unfinish_main where TYPE=3 and JOB_STATE=0 and PREDICT_TIME='" + date + "' ");
        sb.append("        UNION ");
        sb.append("        select person_id PRINCIPAL from t_wrt_unfinish_person where MAIN_ID IN ( ");
        sb.append("                select ID from t_wrt_unfinish_main where TYPE=3 and JOB_STATE=0 and PREDICT_TIME='" + date + "' ");
        sb.append("        ) and PERSON_TYPE=1 ");
        sb.append(") A LEFT JOIN T_SYS_USER B ON A.PRINCIPAL =B.ID ");
        return queryForJsonList(sb.toString());
    }

    /**
     * 关闭原因
     *
     * @param entity
     * @return
     */
    public Integer overWReportUnfinishMain(IBaseEntity entity) {
        int row = update(entity);
        return row;
    }
}
